package com.chanyachting.emall.action.Memberorderservices;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.chanyachting.emall.domain.Emallservices;
import com.chanyachting.emall.domain.Member;
import com.chanyachting.emall.domain.Memberorderservices;
import com.chanyachting.emall.form.MemberorderservicesForm;
import com.chanyachting.emall.serviceImpl.MemberorderservicesBO;
import com.chanyachting.emall.util.DateUtil;
import com.chanyachting.emall.util.RequestUtil;

public class MemberorderservicesAction extends DispatchAction {

	
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		ru.setAttribute("mosList", Memberorderservices.list());
		
		return mapping.findForward("list");
	}
	
	
	
	public ActionForward listByMember(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int memberId=Integer.parseInt(ru.getParameter("memberId", "0"));
		
		ru.setAttribute("mosList", Memberorderservices.listByMemberId(memberId));
		
		return mapping.findForward("list");
	}
	
	
	
	//在线支付
	public ActionForward onlinePay(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		Memberorderservices mos=new Memberorderservices(id);
		
		mos.setPayDate(DateUtil.getDate());  //支付时间
		
		mos.setNowState("01"); //当前状态，开启
		
		//到期时间
		String nday=DateUtil.getNMonth1(mos.getPayDate(), mos.getServiceMonthLimit());
		
		mos.setLostDate(nday);      
		
		if(mos.update()){
			
			ru.setSuccessMsg();
			
		}else{
			
			ru.setFailMsg();
		}
		
		return mapping.findForward("onlinePay");
	}
	
	
	//开启功能服务
	public ActionForward open(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int esId=Integer.parseInt(ru.getParameter("esId", "0"));
		
		int memberId=Integer.parseInt(ru.getParameter("memberId", "0"));
		
		//获得功能服务对象
		Emallservices es=new Emallservices(esId);
		
		//获得会员对象
		Member member=new Member(memberId);
		
		//创建会员订购功能服务对象
		Memberorderservices mos=new Memberorderservices(member,es);
		
		mos.setPayDate(DateUtil.getDate());  //支付时间,默认为当前时间
		
		mos.setNowState("01"); //当前状态，开启
		
		//到期时间
		
		String nday=DateUtil.getNMonth1(mos.getPayDate(), mos.getServiceMonthLimit());
		
		mos.setLostDate(nday);   
		
		System.out.println("LostDate:"+mos.getLostDate());
		
		//如果没有重复，并保存成功
		if(!new MemberorderservicesBO(mos).checkExists() && mos.save()){
			
			ru.setSuccessMsg();
		}else{
			
			ru.setMsg("功能/服务不可重复添加！");
		}
		
		
		return mapping.findForward("open");
	}
	
	
	
	
	public ActionForward create(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int esId=Integer.parseInt(ru.getParameter("esId", "0"));
		
		int memberId=Integer.parseInt(ru.getParameter("memberId", "0"));
		
		//获得功能服务对象
		Emallservices es=new Emallservices(esId);
		
		//获得会员对象
		Member member=new Member(memberId);
		
		
		//创建会员订购功能服务对象
		Memberorderservices mos=new Memberorderservices(member,es);
		
		if(mos.save()){
			
			ru.setSuccessMsg();
		}else{
			
			ru.setFailMsg();
		}
		
		
		return mapping.findForward("create");
	}
	
	
	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id", "0"));
		
		if(request.getMethod().equals("POST")){
			
			MemberorderservicesForm mosForm=(MemberorderservicesForm)form;
			
			if(mosForm.validate(mapping, request).isEmpty()){
				
				Memberorderservices mos=new Memberorderservices(id);
				
				BeanUtils.copyProperties(mos, mosForm);
				
				if(mos.update()){
					
					ru.setSuccessMsg();
				}else{
					
					ru.setFailMsg();
				}
			}
			
		}
		
		ru.setAttribute("memberorderservices", new Memberorderservices(id));

		return mapping.findForward("update");
	}
	
	
	
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		
		
		return mapping.findForward("delete");
	}
	
	
	

}
